#!/usr/bin/env python
import cgi
import os
import wsgiref.handlers

from dataModels import *

from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.ext.db import GqlQuery
from google.appengine.api import users
from google.appengine.ext import webapp

class MainPage(webapp.RequestHandler):
  def get(self):
    self.redirect('/health')
  def post(self):
    self.redirect('/health')

class HealthPage(webapp.RequestHandler):
  def get(self):
    template_values = {
        'medical_condition':HealthCondition.all()
        }
    path = os.path.join(os.path.dirname(__file__),'templates/healthOpt.html')
    self.response.out.write(template.render(path,template_values))

  def post(self):
    self.redirect('/health')

class MenuPage(webapp.RequestHandler):
  def get(self):
    self.redirect('/')
  def post(self):
    cond = HealthCondition(**{'cal':3,'satFat':3,'transFat':3,
      'choles':3,'sodium':3,'fiber':3,'sugar':3,'protien':3})
    for hc in HealthCondition.all():
      if self.request.get(hc.name) == '1':
        cond.cal = min(cond.cal,hc.cal)
        cond.satFat = min(cond.satFat,hc.satFat)
        cond.transFat = min(cond.transFat,hc.transFat)
        cond.choles = min(cond.choles,hc.choles)
        cond.sodium = min(cond.sodium,hc.sodium)
        cond.fiber = min(cond.fiber,hc.fiber)
        cond.sugar = min(cond.sugar,hc.sugar)
        cond.protien = min(cond.protien,hc.protien)
    items = []
    for item in MenuItem.all().order('cat'):
      if item.cal <= cond.cal and item.satFat <= cond.satFat and item.transFat <= cond.transFat and item.choles <= cond.choles and item.sodium <= cond.sodium and item.fiber <= cond.fiber and item.sugar <= cond.sugar and item.protien <= cond.protien:
        items.append(item)

    template_values = {
        'menu_items':items,
        }
    path = os.path.join(os.path.dirname(__file__),'templates/menuOpt.html')
    self.response.out.write(template.render(path,template_values))

class OrderPage(webapp.RequestHandler):
  def get(self):
    self.redirect('/')
  def post(self):
    purch = []
    total = 0
    for k in self.request.arguments():
      if ( self.request.get(k) == '1' ):
        purch.append(MenuItem.get(k))
        total += MenuItem.get(k).cost
    template_values = {
        'items':purch,
        'total':total,
        }
    path = os.path.join(os.path.dirname(__file__),'templates/order.html')
    self.response.out.write(template.render(path,template_values))

class Reciept(webapp.RequestHandler):
  def get(self):
    self.redirect('/')
  def post(self):
    purch = []
    total = 0
    for k in self.request.arguments():
      purch.append(MenuItem.get(k))
      total += MenuItem.get(k).cost
    tax = total * .13
    gtotal = total * 1.13
    template_values = {
        'items':purch,
        'total':total,
        'gtotal':'%.2f'%(gtotal),
        'tax':'%.2f'%(tax),
        }
    path = os.path.join(os.path.dirname(__file__),'templates/reciept.html')
    self.response.out.write(template.render(path,template_values))

class All(webapp.RequestHandler):
  def get(self):
    print 'index.py All'
  def post(self):
    print 'index.py All'

application = webapp.WSGIApplication([
  ('/', MainPage),
  ('/health', HealthPage),
  ('/menu', MenuPage),
  ('/order', OrderPage),
  ('/purch', Reciept),
  ('.*',All),
  ], debug=True)

def main():
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main()
